import 'dart:convert';
import 'dart:convert' as convert;

import 'package:flutter/material.dart';
import 'package:toast/toast.dart';
import 'package:village_android/config/service_method.dart';
import 'package:village_android/main.dart';
import 'package:village_android/model/menu_model.dart';
import 'package:village_android/model/user_model.dart';
import 'package:village_android/pages/article/article_list.dart';
import 'package:village_android/pages/browser.dart';
import 'package:village_android/pages/charge/charge_list.dart';
import 'package:village_android/pages/charge/charge_submit_page.dart';
import 'package:village_android/pages/health/health_manager_page.dart';
import 'package:village_android/pages/health/health_monitor_public_page.dart';
import 'package:village_android/pages/health/health_submit_record_page.dart';
import 'package:village_android/pages/monitor/monitor_report_select_page.dart';
import 'package:village_android/pages/order/order_list_page.dart';
import 'package:village_android/pages/point/health_point_page.dart';
import 'package:village_android/pages/health/health_submit_mutils_list.dart';
import 'package:village_android/pages/health/rental_manage_page.dart';
import 'package:village_android/pages/index/charge_manager_page.dart';
import 'package:village_android/pages/index/finance_manager_page.dart';
import 'package:village_android/pages/index/jijianjiancha_manager_page.dart';
import 'package:village_android/pages/monitor/monitor_report_list_page.dart';
import 'package:village_android/pages/party/party_learning_page.dart';
import 'package:village_android/pages/party/party_manager_page.dart';
import 'package:village_android/pages/party/party_pay_page.dart';
import 'package:village_android/pages/party/party_sanhuiyike_page.dart';
import 'package:village_android/pages/party/party_zhendi_page.dart';
import 'package:village_android/pages/village/village_manager_page.dart';
import 'package:village_android/pages/village/village_zhenxing_page.dart';
import 'package:village_android/utils/store_utils.dart';
import 'package:fluwx/fluwx.dart' as fluwx;

typedef ValueCallback = void Function(Object object, bool isIncrease);
typedef ValueSingleCallback = void Function(Object object);

class CommonUtils {
  static Future showToast(BuildContext context, String msg) {
    Toast.show(msg, context, duration: 2, gravity: Toast.BOTTOM);
  }

  static Future getUserInfo() async {
    var userId = sp.get(SharedPreferenceUtil.USER_ID);
    if (userId == null) {
      return null;
    }
    var realData = await getUserData(userId);
    SharedPreferenceUtil.save(SharedPreferenceUtil.KEY_USERMODEL, realData);
    return realData;
  }

  static String getUserId() {
    String userId = sp.get(SharedPreferenceUtil.USER_ID);
    return userId;
  }

  static String getUsername() {
    UserModel user = getLocalUserInfo();
    if(user==null) return null;
    return user.account + '-' + user.name;
  }

  static UserModel getLocalUserInfo() {
    var data = sp.get(SharedPreferenceUtil.KEY_USERMODEL);
    if (data == null) {
      return null;
    }
    var datajson = convert.jsonDecode(data);
    UserModel userModel = UserModel.fromJson(datajson);
    return userModel;
  }

  static bool pushPage(context, item) {
    var page;
    String param = item["param"];
    var type = 0;
    var module = 0;
    var url = "";
    if (param.length > 0) {
      var dict = json.decode(param);
      type = dict["type"];
      module = dict["module"];
      url = dict["url"];
    }

    String title = item["title"];
    url = item["url"];
    var router = item['router'];
    if (router.length == 0) {
      return false;
    }
    MenuModel menuModel = MenuModel(title, type, module, router);
    if (router == 'ArticleList') {
      page = new ArticleListPage(
        menuModel: menuModel,
      );
    }
    if (router == 'RentalManager') {
      page = new RentalManagerPage(
        menuModel: menuModel,
      );
    }
    if (router == 'HealthManager') {
      page = new HealthManagerPage(
        menuModel: menuModel,
      );
    }
    if (router == 'HealthSubmitPage') {
      page = new HealthSubmitMutilsPage();
    }
    if (router == 'HealthSubmitRecordPage') {
      page = new HealthSubmitRecordPage();
    }

    if (router == 'OrderListPage') {
      page = new OrderListPage(menuModel: menuModel,);
    }
    if (router == 'HealthPoint') {
      page = new HealthPointPage();
    }
    if (router == 'HealthMonitorPublic') {
      page = new HealthMonitorPublicPage(
        menuModel: menuModel,
      );
    }
    if (router == 'VillageManager') {
      page = new VillageManagerPage(
        menuModel: menuModel,
      );
    }
    if (router == 'FinanceManager') {
      page = new FinanceManagerPage(
        menuModel: menuModel,
      );
    }
    if (router == 'PartyManager') {
      page = new PartyManagerPage(
        menuModel: menuModel,
      );
    }
    if (router == 'PartyLearning') {
      page = new PartyLearningPage(
        menuModel: menuModel,
      );
    }
    if (router == 'Sanhuiyike') {
      page = new PartySanhuiyikePage(
        menuModel: menuModel,
      );
    }
    if (router == 'PartyZhendi') {
      page = new PartyZhendiPage(
        menuModel: menuModel,
      );
    }
    if (router == 'PartyPay') {
      page = new PartyPayPage();
    }
    if (router == 'MonitorReportSelect') {
      page = new MonitorReportSelectPage();
    }
    if (router == 'MonitorReport') {
      page = new MonitorReportListPage(
        menuModel: menuModel,
      );
    }
    if (router == 'HealthPoint') {
      page = new HealthPointPage();
    }
    if (router == 'ChargeManager') {
      page = new ChargeManagerPage(
        menuModel: menuModel,
      );
    }
    if (router == 'ChargeList') {
      page = new ChargeListPage(
        menuModel: menuModel,
      );
    }
    if (router == 'JijianjianchaManager') {
      page = new JijianjianchaManagerPage(
        menuModel: menuModel,
      );
    }
    if (router == 'ChargeSubmit') {
      page = new ChargeSubmitPage(
        menuModel: menuModel,
      );
    }

    if (router == 'VillageZhenxing') {
      page = new VillageZhenxingPage(
        menuModel: menuModel,
      );
    }

    if (router == 'ArticleDetail') {
      page = new BrowserPage(
        title: title,
        url: url,
      );
    }

    if(router == 'MiniProgram') {
      fluwx.launchWeChatMiniProgram(username: "gh_f008c89f6c98").then((data) {
        print(data);
      });
    }

    //page
    if (page == null) {
      Toast.show("功能待实现", context, duration: 2, gravity: Toast.BOTTOM);
      return false;
    }

    Navigator.push(context, new MaterialPageRoute(builder: (context) => page));
    return true;
  }
}
